﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;



namespace config
{
    public partial class frmConfig : Form
    {
        ArrayList camara;

        public frmConfig()
        {
            InitializeComponent();
        }

        private void btnExaminar_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmConfig_Load(object sender, EventArgs e)
        {
            camara = new ArrayList();
            string stringPath = Application.StartupPath;
            textBox1.Text = @"C:\DvrWebCam";
            if (File.Exists(stringPath + @"\config.log"))
            {
                string line;
                System.IO.StreamReader file = new System.IO.StreamReader(stringPath + @"\config.log");
                while ((line = file.ReadLine()) != null)
                {
                    camara.Add(line);  
                }
                file.Close();
            }
            if (camara[0].ToString() == "0") chkCam0.Checked = false;
            else chkCam0.Checked = true;
            if (camara[1].ToString() == "0") chkCam1.Checked = false;
            else chkCam1.Checked = true;
            if (camara[2].ToString() == "0") chkCam2.Checked = false;
            else chkCam2.Checked = true;
            if (camara[3].ToString() == "0") chkCam3.Checked = false;
            else chkCam3.Checked = true;
            textBox1.Text = camara[4].ToString();
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            string stringPath = Application.StartupPath;
            StringBuilder sb = new StringBuilder();

            if (!chkCam0.Checked) camara[0] = "0";
            else camara[0] = "1";

            if (!chkCam1.Checked) camara[1] = "0";
            else camara[1] = "1";

            if (!chkCam2.Checked) camara[2] = "0";
            else camara[2] = "1";

            if (!chkCam3.Checked) camara[3] = "0";
            else camara[3] = "1";

            camara[4] = textBox1.Text;
            foreach (string n in camara)
            {
                sb.AppendLine((string)n);
            }
            using (StreamWriter outfile = new StreamWriter(stringPath + @"\config.log")) // escribo el archivo .bat
            {
                outfile.Write(sb.ToString());
            }
            MessageBox.Show("Debe reiniciar Trend DVR WebCam para aplicar los cambios.", "Trend DVR WebCam", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Application.Exit();
        }
    }
}
